package com.gliese.bambook.app;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import com.gliese.bambook.handler.NewsAnalyzeHandler;
import com.gliese.bambook.handler.SnbcAndSnbfHandler;
import com.gliese.bambook.object.Snbc;
import com.gliese.bambook.service.snb.interfaces.PackageSnbService;
import com.gliese.bambook.util.HtmlToXmlUtils;

/**
 * serviceType : sinaNewsAnalysisService relate to bean Name
 */
public class SinaBambookApp {
	final static Log log = LogFactory.getLog(SinaBambookApp.class);

	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
				"beans.xml"));
		NewsAnalyzeHandler newsAnalyzeHandler = (NewsAnalyzeHandler) beanFactory
				.getBean("newsAnalyzeHandler");
		SnbcAndSnbfHandler snbcAndSnbfHandler = (SnbcAndSnbfHandler) beanFactory
				.getBean("snbcAndSnbfHandler");
		PackageSnbService packageSnbService = (PackageSnbService) beanFactory
				.getBean("packageSnbService");
		List<String> urls = new ArrayList<String>();
		try {
			urls = getListOfNewsSina();
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		boolean first = true;
		int chapterth = 1;
		for (String url : urls) {
			Snbc snbc = newsAnalyzeHandler.analyzeHandle(
					"sinaNewsAnalysisService", url);

			snbcAndSnbfHandler.Handle(snbc, UUID.randomUUID().toString()
					+ ".snbc", chapterth, first);
			first = false;
			chapterth++;
		}
		Snbc snbc = new Snbc();
		snbc.setUrl("news.sina.com.cn");
		packageSnbService.packageSnb(snbc);
	}

	/**
	 * get the list of news http://news.sina.com.cn/china/2011-2-13/index.shtml
	 */
	public static List<String> getListOfNewsSina() throws Exception {
		List<String> urls = new ArrayList<String>();
		// set url
		Calendar calendar = Calendar.getInstance();
		/*
		 * String url = "http://news.sina.com.cn/china/" +
		 * calendar.get(Calendar.YEAR) + "-" + (1 +
		 * calendar.get(Calendar.MONTH)) + "-" +
		 * calendar.get(Calendar.DAY_OF_MONTH) + "/index.shtml";
		 */
		String url = "http://news.sina.com.cn/china/";
		// set HttpClient
		HttpClient httpClient = new HttpClient();
		HttpMethod httpMethod = new GetMethod(url);
		String httpHtml = HtmlToXmlUtils.getHtmlCodeByHttpMethod(httpClient,
				httpMethod);
		httpMethod.releaseConnection();
		/**
		 * xml analysis
		 */
		SAXReader saxReader = new SAXReader();
		System.out.println(httpHtml);
		Document document = saxReader.read(new StringReader(httpHtml));
		List<?> listOfNews = document
				.selectNodes("id('data0_0')/li/a");
		for (Object o : listOfNews) {
			Element element = (Element) o;
			urls.add(element.attribute("href").getValue());

		}
		return urls;
	}

}
